<?php

namespace app\company\controller\crisis_intervention\config;

use app\BaseController;
use app\model\CrisisBehaviorModel;
use app\model\CrisisLevelModel;
use app\model\CrisisMeasuresModel;
use app\model\CrisisTypeModel;
use app\model\DepartmentModel;
use app\model\FollowUserModel;
use app\model\InterfereUserModel;
use app\model\RatingScaleTypeModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 干预措施选择器
     * @return bool|string
     */
    public function CrisisMeasuresSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $where = [];
            $where[] = ['unit_id', '=', $param['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisMeasuresModel = new CrisisMeasuresModel();
            $data = $CrisisMeasuresModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常行为表现选择器
     * @return bool|string
     */
    public function CrisisBehaviorSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $where = [];
            $where[] = ['unit_id', '=', $param['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            $data = $CrisisBehaviorModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 风险等级评估选择器
     * @return bool|string
     */
    public function CrisisLevelSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $where = [];
            $where[] = ['unit_id', '=', $param['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisLevelModel = new CrisisLevelModel();
            $data = $CrisisLevelModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 疑似问题类型选择器
     * @return bool|string
     */
    public function CrisisTypeSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $where = [];
            $where[] = ['unit_id', '=', $param['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisTypeModel = new CrisisTypeModel();
            $data = $CrisisTypeModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 关注人群-搜索栏
     * @return bool|string
     */
    public function FollowSearch(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $FollowUserModel = new FollowUserModel();
            $data['up_num'] = $FollowUserModel->where('unit_id', '=', $param['unit_id'])->order('up_num desc')->value('up_num');
            $operator_uid = $FollowUserModel->where('unit_id', '=', $param['unit_id'])->column('operator_uid');
            $operator_uid = array_unique($operator_uid);
            $UserModel = new UserModel();
            $data['operator'] = $UserModel->where('id', 'in', $operator_uid)->field('id,name')->select()->toArray();
            $uids = $FollowUserModel->where('unit_id', '=', $param['unit_id'])->column('uid');
            $department_id = $UserModel->where('id', 'in', $uids)->column('department_id');
            $department_id = array_unique($department_id);
            $DepartmentModel = new DepartmentModel();
            $data['department'] = $DepartmentModel->where('id', 'in', $department_id)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预人群-搜索栏
     * @return bool|string
     */
    public function InterfereSearch(): bool|string
    {
        $param = $this->request->param();
        $this->validate($param,[
            'unit_id|单位ID'  =>  'require',
        ]);
        if(!isset($param['status'])){
            $where[] = ['status','=',0];
        }else{
            $where[] = ['status','=',1];
        }
        $InterfereUserModel = new InterfereUserModel();
        $UserModel = new UserModel();
        $where[] = ['unit_id','=',$param['unit_id']];
        $uids = $InterfereUserModel->where($where)->column('uid');
        $department_id = $UserModel->where('id', 'in', $uids)->column('department_id');
        $department_id = array_unique($department_id);
        $DepartmentModel = new DepartmentModel();
        $data['department'] = $DepartmentModel->where('id', 'in', $department_id)->select()->toArray();
        return success($data);
    }

    /**
     * 量表选择器
     * @return bool|string
     */
    public function ratingScaleSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $where = [];
            $RatingScaleWhere = [];
            if (!empty($param['name'])) {
                $RatingScaleWhere[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            $where[] = ['unit_id', '=', $param['unit_id']];
            $where[] = ['status', '=', 1];
            $RatingScaleTypeModel = new RatingScaleTypeModel();
            $list = $RatingScaleTypeModel->field('id,name')
                ->where($where)
                ->with([
                    'RatingScale' => function ($query) use ($RatingScaleWhere) {
                        $query->where($RatingScaleWhere)->field('id,type_id,name,mode_id');
                    }
                ])->select()->toArray();
            return success($list);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


}